package com.controller;


import com.github.pagehelper.PageInfo;
import com.pojo.*;
import com.service.SiteService;
import com.vo.Result;
import com.vo.SiteProjectVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/Site")
public class SiteProjectController {
    @Resource
    private SiteService siteService;

    @RequestMapping("/SiteProject/index")
    public String toSite(){
        return "/Site/SiteProject";
    }

    @RequestMapping("/SiteProject/show")
    @ResponseBody
    public Result selectSiteProject(@RequestBody SiteProjectVo siteProjectVo){
        PageInfo<Siteproject> siteprojectPageInfo = siteService.selectAllSiteProject(siteProjectVo);
        return Result.success(siteprojectPageInfo);
    }

    //选择制定人
    @RequestMapping("/SiteProject/showprducer")
    @ResponseBody
    public Result selectSiteProjectproducer(){
        List<User> users = siteService.selectAllUser(1);
        return Result.success(users);
    }

    //查询维修部的巡查人员
    @RequestMapping("/SiteProject/showusermc")
    @ResponseBody
    public Result selectSiteProjectUserMc(){
        Integer id=1;
        Integer ids=3;
        List<User> users = siteService.selectUsermcA(id, ids);
        return Result.success(users);
    }

    @RequestMapping("/SiteProject/update")
    @ResponseBody
    public Result update(Siteproject siteproject){
        try {
            Siteroute siteroute=new Siteroute();
            siteroute.setId(siteproject.getRouteid());
            siteroute.setStartend(2);
            siteService.updateSiteRoute(siteroute);


            siteService.updateSiteProject(siteproject);

            int selectquestidbyprojectidtoupdate = siteService.selectquestidbyprojectidtoupdate(siteproject.getId());
            Sitequest sitequest=new Sitequest();

            sitequest.setId(selectquestidbyprojectidtoupdate);
            sitequest.setProjectid(siteproject.getId());
            sitequest.setPeopstartandend(siteproject.getTimeradius());
            sitequest.setProjectpeople(siteproject.getUsers());
            sitequest.setNowpeople(siteproject.getUsers());
            sitequest.setSiteway(siteproject.getSigntype());
            sitequest.setQiyong(siteproject.getState());


            siteService.updateSiteQuest(sitequest);

            return Result.success("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("修改失败");
        }
    }
    @RequestMapping("/SiteProject/add")
    @ResponseBody
    public Result add(Siteproject siteproject){
        try {
            Siteroute siteroute=new Siteroute();

            siteroute.setId(siteproject.getRouteid());
            siteroute.setStartend(2);

            siteService.updateSiteRoute(siteroute);


            siteService.addSiteProject(siteproject);

            Sitequest sitequest=new Sitequest();

            sitequest.setProjectid(siteproject.getId());
            sitequest.setPeopstartandend(siteproject.getTimeradius());
            sitequest.setProjectpeople(siteproject.getUsers());
            sitequest.setNowpeople(siteproject.getUsers());
            sitequest.setSiteway(siteproject.getSigntype());
            sitequest.setState(1);
            sitequest.setQiyong(1);

            siteService.addSiteQuest(sitequest);


            //明细添加
            Siteids siteids=new Siteids();

            siteids.setProjectid(siteproject.getId());
            siteids.setRouteid(siteproject.getRouteid());

            List<Integer> selectpointidbyrouteid = siteService.selectpointidbyrouteid(siteproject.getRouteid());

            for (Integer integer : selectpointidbyrouteid) {
                siteids.setPointid(integer);

                siteService.addsiteids(siteids);
            }


            return Result.success("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("添加失败");
        }
    }


    @RequestMapping("/SiteProject/delete")
    @ResponseBody
    public Result delete(Integer id){
        try {
            Integer selectsitequestdeleteid = siteService.selectsitequestdeleteid(id);
            Siteroute siteroute=new Siteroute();
            siteroute.setId(selectsitequestdeleteid);
            siteroute.setStartend(1);
            siteService.updateSiteRoute(siteroute);

            siteService.selectmingxiidbyid(id);

            List<Integer> selectpointid = siteService.selectpointid(id);

            for (Integer integer : selectpointid) {
                Sitepoint sitepoint=new Sitepoint();
                sitepoint.setId(integer);
                sitepoint.setPointstate(1);
                sitepoint.setAttachpath(" ");
                sitepoint.setRealsitetime(" ");
                siteService.updateSitepoint(sitepoint);
            }

//根据巡检计划id 删除巡检任务
            Integer selectquestidbyprojectidtoupdate = siteService.selectquestidbyprojectidtoupdate(id);
            if (selectquestidbyprojectidtoupdate !=0 && selectquestidbyprojectidtoupdate!=null){
                siteService.deleteSiteQuest(selectquestidbyprojectidtoupdate);
            }else {

            }

            siteService.deleteSiteProject(id);
            return Result.success("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("删除失败");
        }
    }

    //修改  启用/停用  状态
    @RequestMapping("/SiteProject/updateType")
    @ResponseBody
    public Result updateType(Integer id){
        try {
            Integer integer = siteService.selelectSiteProjectbyid(id);

            Siteproject siteproject = new Siteproject();
            siteproject.setId(id);

            int selectquestidbyprojectidtoupdate = siteService.selectquestidbyprojectidtoupdate(id);
            Sitequest sitequest=new Sitequest();

            sitequest.setId(selectquestidbyprojectidtoupdate);

            if (integer==1){
                siteproject.setState(2);
                siteService.updateSiteProjectbyid(siteproject);
                sitequest.setQiyong(2);
                siteService.updateSiteQuest(sitequest);
            }else {
                siteproject.setState(1);
                siteService.updateSiteProjectbyid(siteproject);
                sitequest.setQiyong(1);
                siteService.updateSiteQuest(sitequest);
            }
            return Result.success("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("修改失败");
        }
    }
}
